<template>
  <div class="dialog-form">
    <el-form ref="projectRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="检查标准" prop="standard">
        <el-input v-model="form.standard" type="textarea" :rows="12" :maxlength="1000" show-word-limit placeholder="请输入检查标准" />
      </el-form-item>
      <el-form-item label="项目状态" prop="status" required>
        <el-switch
          v-model="form.status"
          inline-prompt
          :active-value="1"
          :inactive-value="2"
          inactive-text="禁用"
          active-text="启用"
        />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getPatrolProject, addPatrolProject, updatePatrolProject } from "@/api/patrol/project"

const projectRef = ref()
const { proxy } = getCurrentInstance()
const { bus_enabled } = proxy.useDict('bus_enabled')

const emits = defineEmits(['cancel', 'confirm'])

const form = ref({
  id: null,
  name: null,
  standard: null,
  status: 1
})
const rules = {
  name: [
    { required: true, message: "项目名称不能为空", trigger: "blur" }
  ],
  standard: [
    { required: true, message: "检查标准不能为空", trigger: "blur" }
  ]
}

const pageInit = (id) => {
  if (id) {
    getPatrolProject(id).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  projectRef.value.validate(valid => {
    if (valid) {
      if (form.value.id) {
        updatePatrolProject(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          emits('confirm')
        })
      } else {
        addPatrolProject(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>